package com.malou.mventas.server.login;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.http.HttpSession;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

import com.malou.mventas.client.login.LoginService;
import com.malou.server.base.Conexion;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class LoginServiceImpl extends RemoteServiceServlet implements LoginService {

	private static final long serialVersionUID = 1L;

	@Override
	public String isValidUser(String user, String pw) {
		
		if(Usuarios.validarresponsable(user, pw)){
			HttpSession httpSession = getThreadLocalRequest().getSession();  
		    httpSession.setAttribute("m_username", user);		    
			return Usuarios.getTiendas();
		}
		else
			return null;
	}

	@Override
	public void logout() {
		
		HttpSession httpSession = getThreadLocalRequest().getSession(false);	    
		httpSession.invalidate();
	}

	@Override
	public String issetUser() {
		HttpSession session = getThreadLocalRequest().getSession();
		if (session.getAttribute("m_username") != null){			
	        return Usuarios.getTiendas();  
	    }else{
	    	return null;
	    }
	}

	@Override
	public String getHtmlReport(String fileName, HashMap<String,Object> param,	Integer consulta_id) {
		
		
		String filePath = getServletConfig().getServletContext().getRealPath(fileName);		
		try {			
			
			JasperPrint print = getJasperPrint(fileName, param, consulta_id);			
			String newFileName = filePath+".html";
			JasperExportManager.exportReportToHtmlFile(print, newFileName);			
			
		} catch (SQLException e) {			
			e.printStackTrace();
		}catch (JRException e) {			
			e.printStackTrace();
		}
		
		return fileName+".html";
	}
	
	private JasperPrint getJasperPrint(String fileName, HashMap<String, Object>  param, Integer consulta_id) throws SQLException, JRException{
		

		try {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String desde = param.get("desde").toString();
			param.put("desde", df.parse(desde));
			if(param.get("hasta") != null){
				String hasta = param.get("hasta").toString();
				param.put("hasta", df.parse(hasta));
			}
						
		} catch (ParseException e) {			
			e.printStackTrace();
		}
		
		String filePath = getServletConfig().getServletContext().getRealPath(fileName);
		Connection con;
		JasperPrint print=null;
		
		con = Conexion.getSqlMap().getDataSource().getConnection();		
		Integer tienda_id = Integer.parseInt(param.get("tienda_id").toString()); 
		preConsultas(tienda_id, param, consulta_id);		
		print = JasperFillManager.fillReport(filePath+".jasper", param, con);
		
		return print;		
	}
	
	private void preConsultas(Integer tienda_id, HashMap<String, Object> param,Integer consulta_id){
		if(consulta_id != 0)
		if(consulta_id == 1){
			Usuarios.setActualStock(tienda_id, (Date)param.get("desde"));
		}
	}

	@Override
	public String getBalanceTienda(String fecha, Integer tienda_id) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date fechaactual = null;
		try {
			fechaactual = df.parse(fecha);
		} catch (ParseException e) {			
			e.printStackTrace();
		}
		return Usuarios.getBalanceTienda(fechaactual,tienda_id);
	}
}
